﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Threading;
using System.Globalization;

namespace Fraktalysator
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			//Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
			//Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
			//AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
			//throw new Exception("Hallo");
		}

		static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
		{
			string filename = FileSystem.FileExceptionLog;
			using (StreamWriter writer = new StreamWriter(filename, true))
			{
				DateTime dt = DateTime.Now;
				string date = dt.ToShortDateString() + ' ' + dt.ToLongTimeString();
				writer.WriteLine(date);
				writer.WriteLine(new string('-', date.Length));
				writer.WriteLine(e.ExceptionObject.ToString());
				writer.WriteLine();
				writer.Flush();
			}
			MessageBox.Show(string.Format(Localization.Text.MessageBox_UnhandledException, filename), Localization.Text.MessageBox_UnhandledExceptionCaption);
		}

		//static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
		//{
		//	AssemblyName aName = new AssemblyName(args.Name);
		//	string resName = aName.Name.Replace('.', '_');
		//	//if (resName == "Fraktalysator_resources")
		//	//	return null;
		//	if (args.RequestingAssembly == null)
		//	{
		//		return null;
		//	}
		//	byte[] rawAssembly = (byte[])AssemblyResources.Assemblies.ResourceManager.GetObject(resName);
		//	if (rawAssembly == null)
		//		return null;
		//	return Assembly.Load(rawAssembly);
		//}
	}
}
